fix: avoid mutating query config - #3720
Conversation
Fixes brianc#2651. normalizeQueryConfig() wrote values/callback directly onto the object it was given instead of copying it first. Reusing the same config object across calls (callback style, then promise style) left a stale callback on it, so a later promise-style call would see query.callback already set, skip creating the promise, and silently return undefined while the real result went to the old callback instead. Copy the object before writing to it so the caller's config is never touched, and add regression tests for both the direct case and the callback-then-promise reuse scenario.
| // can take in strings or config objects | ||
| config = typeof config === 'string' ? { text: config } : config | ||
| // Copy config so normalization does not mutate the caller's object. | ||
| config = typeof config === 'string' ? { text: config } : { ...config } |
There was a problem hiding this comment.
This is a potential breaking change for query objects that don’t have own enumerable query config properties. I’d say return an object like { callback: config.callback, values: config.values, config } and have callers access normalizedConfig.config?.otherProperty, but that would be a breaking change for normalizeQueryConfig itself, so… another function?
There was a problem hiding this comment.
Thanks, good catch. I switched this away from object spread so it keeps the original prototype and property descriptors while still avoiding mutation of the caller's object.
I added a unit test with an inherited text getter for this case. Also ran make test-unit, yarn lint, and yarn build.
|
groovy! thanks for the code AND for the existing PR review to catch a potentially breaking change. 😄 |
Fixes #2651
client.query()addscallbackandvaluesto config objects passed by the caller. Reusing the same object can make a later promise-style call returnundefined.Copy the config before normalizing it and add regression tests for config reuse and mutation.